### packages
library(dplyr)
library(ggplot2)
library(survey)
library(ggsurvey)
library(patchwork)

########################
# Load and set up data #
########################
## load  data
# France
dat.rcll.fr <- read.csv("France - recall.csv")
# Israel
dat.rcll.il <- read.csv("Israel - recall.csv")
# Sweden
dat.rcll.sw <- read.csv("Sweden - recall.csv")

## remove "NA" values on recall question
dat.rcll.fr.nona <- filter(dat.rcll.fr,!is.na(recall))
dat.rcll.il.nona <- filter(dat.rcll.il,!is.na(recall))
dat.rcll.sw.nona <- filter(dat.rcll.sw,!is.na(recall))

## define category order in recall question
dat.rcll.fr.nona$recall <- factor(dat.rcll.fr.nona$recall, levels = c("Correct","Wrong","Don't Know"))
dat.rcll.il.nona$recall <- factor(dat.rcll.il.nona$recall, levels = c("Correct","Wrong","Don't Know"))
dat.rcll.sw.nona$recall <- factor(dat.rcll.sw.nona$recall, levels = c("Correct","Wrong","Don't Know"))

#######################################
# plot answer distribution (Figure 5) #
#######################################
# define survey design with weights
wgt.fr <- svydesign(id = ~1, weights = ~weightvec, data = dat.rcll.fr.nona)
wgt.il <- svydesign(id = ~1, weights = ~weightvec, data = dat.rcll.il.nona)
wgt.sw <- svydesign(id = ~1, weights = ~weightvec, data = dat.rcll.sw.nona)
# france
plot.rcll.fr <- ggbarweight_svy(wgt.fr,recall) + 
  geom_hline(aes(yintercept=0.5), linetype = "dashed") + 
  labs(title = "France", y = "Share of Respondents") + 
  scale_y_continuous(labels=scales::percent, limits = c(0,0.8), breaks = seq(0,0.75,0.25)) +
  scale_x_discrete(labels=c("Correct", "Wrong", "Don't\nKnow")) +
  theme_bw() +
  theme(
    panel.grid = element_blank(),
    axis.title.x = element_blank(),
    axis.text.x = element_text(size=11),
    legend.position="bottom",
  )
# Israel
plot.rcll.il <- ggbarweight_svy(wgt.il,recall) + 
  geom_hline(aes(yintercept=0.5), linetype = "dashed") + 
  labs(title = "Israel", y = "Share of Respondents") + 
  scale_y_continuous(labels=scales::percent, limits = c(0,0.8), breaks = seq(0,0.75,0.25)) +
  scale_x_discrete(labels=c("Correct", "Wrong", "Don't\nKnow")) +
  theme_bw() +
  theme(
    panel.grid = element_blank(),
    axis.title = element_blank(),
    axis.text.x = element_text(size=11),
    legend.position="bottom",
  )
# Sweden
plot.rcll.sw <- ggbarweight_svy(wgt.sw,recall) + 
  geom_hline(aes(yintercept=0.5), linetype = "dashed") + 
  labs(title = "Sweden", y = "Share of Respondents") + 
  scale_y_continuous(labels=scales::percent, limits = c(0,0.8), breaks = seq(0,0.75,0.25)) +
  scale_x_discrete(labels=c("Correct", "Wrong", "Don't\nKnow")) +
  theme_bw() +
  theme(
    panel.grid = element_blank(),
    axis.title = element_blank(),
    axis.text.x = element_text(size=11),
    legend.position="bottom",
  )
# combine graph
plot.rcll.all <- plot.rcll.fr + plot.rcll.il + plot.rcll.sw + 
  plot_layout(axis_titles  = "collect")
# save plot (Figure 5)
ggsave(file = "Figure5.pdf", plot=plot.rcll.all, width=8, height=3.5)
